home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmtring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  3.2 KB  |  76 lines

  1. // CmTRing.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Ring (circular linked list) template definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMTRING_H
  10. #define _CMTRING_H
  11.  
  12. #include <cm/include/cmtcont.h>
  13.  
  14. template <class T> class CmTRingIterator;    // Ring iterator class stub.
  15. template <class T> class CmTRingNode;        // Ring node class stub.
  16.  
  17. template <class T> class CmTRing : public CmTContainer<T> {
  18. public:
  19.   CmTRing() : _top(NULL), _last(NULL) {}     // Default ring constructor.
  20.   CmTRing(const CmTRing<T>&);                // Ring copy constructor.
  21.  ~CmTRing();                                 // Ring destructor.
  22.  
  23.   CmTRing<T>& operator=(const CmTRing<T>&);  // Assignment operator.
  24.  
  25.   Bool     add        (const T&);            // Add item to ring top.
  26.   Bool     remove     (const T&);            // Remove equal item.
  27.   Bool     removeTop  ();                    // Remove top object.
  28.   const T& lookup     (const T&) const;      // Return equal item.
  29.   Bool     contains   (const T&) const;      // See if ring contains equal.
  30.   unsigned occurrences(const T&) const;      // Count number of equal objects.
  31.   void     removeAll  ();                    // Remove all objects from ring.
  32.   const T& top        () const;              // Get pointer to top object.
  33.  
  34.   CmTIterator<T>* newIterator() const;       // Create and return iterator.
  35.  
  36. protected:
  37.   CmTRingNode<T> *_top;                      // Top node in ring.
  38.   CmTRingNode<T> *_last;                     // Last node in ring.
  39.   friend          CmTRingIterator<T>;        // Iterator can access.
  40. };
  41.  
  42. template <class T> class CmTRingIterator : public CmTIterator<T> {
  43. public:
  44.   CmTRingIterator(const CmTRing<T>& R)       // Iterator constructor.
  45.                   : _ring(R), _node(R._top) {}
  46.  
  47.   Bool     done    () const;                 // See if done iterating (FALSE).
  48.   const T& next    ();                       // Get next object in ring.
  49.   const T& previous();                       // Return and backup.
  50.   const T& current () const;                 // Get current ring item.
  51.   void     first   ();                       // Move to first item.
  52.   void     last    ();                       // Move to last item.
  53.  
  54. protected:
  55.   CmTRingNode<T>   *_node;                   // Current ring node.
  56.   const CmTRing<T> &_ring;                   // Ring being iterated.
  57.   friend            CmTRing<T>;              // Ring class can access.
  58. };
  59.  
  60. template <class T> class CmTRingNode {       // Ring node definition.
  61. protected:
  62.   CmTRingNode(const T& O)                    // Ring node constructor.
  63.               : _next(NULL), _data(O) {}
  64.  
  65.   CmTRingNode<T> *_next;                     // Next node in ring.
  66.   T               _data;                     // Object.
  67.   friend          CmTRing<T>;                // Ring class can access.
  68.   friend          CmTRingIterator<T>;        // Ring iterator can access.
  69. };
  70.  
  71. #if defined(__TURBOC__) || defined(__xlC__)
  72. #include <cm/include/cmtring.cc>
  73. #endif
  74.  
  75. #endif
  76.